package csu.web.credit_bank.controller;

import csu.web.credit_bank.pojo.UserLog;
import csu.web.credit_bank.service.UserLogsService;
import csu.web.credit_bank.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/logs")
public class UserLogsController {

    @Autowired
    private UserLogsService userLogsService;

    @PostMapping
    public Result<UserLog> createLog(@RequestBody UserLog userLog) {
        try {
            UserLog createdLog = userLogsService.createLog(userLog);
            return createdLog != null ? Result.success("日志记录成功", createdLog) : Result.failure("日志记录失败(ID池可能为空)");
        } catch (Exception e) {
            return Result.failure(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result<UserLog> getLogById(@PathVariable String id) {
        UserLog log = userLogsService.getLogById(id);
        return log != null ? Result.success(log) : Result.failure("日志未找到");
    }

    @GetMapping
    public Result<IPage<UserLog>> getLogList(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int size) {
        return Result.success(userLogsService.page(new Page<>(page, size)));
    }

    // 通常不提供日志的更新操作

    @DeleteMapping("/{id}")
    public Result<Void> deleteLog(@PathVariable String id) {
        return userLogsService.deleteLog(id) ? Result.success("日志删除成功") : Result.failure("日志删除失败");
    }
}
